package com.ale.oxetool.util;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by guoshunw on 2015/10/14.
 */
public class Configuration {
    private Properties props = new Properties();
    private static final String CONFIG_NAME = "/config.properties";

    private File configFile;

    private Configuration() {
        InputStream is = null;
        try {
            configFile = new File("./config.properties");
            is = configFile.exists() ? new FileInputStream(configFile) : Configuration.class.getResourceAsStream(CONFIG_NAME);
            props.load(is);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, String.format("系统配置文件%s不存在，请检查！", CONFIG_NAME), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(-1);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    private static Configuration me = new Configuration();

    public static Configuration getInstance() {
        return me;
    }

    public String get(String key) {
        return props.getProperty(key);
    }

    public void set(String key, String value) {
        props.setProperty(key, value);
    }
}
